package com.service;

import com.bean.WorkLogInfo;
import com.bean.other.MetaInfo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mapper.WorkLogInfoMapper;
import com.mapper.repository.WorkLogInfoRepository;
import com.service.impl.WorkLogInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.UUID;

@Service
public class WorkLogInfoServiceImpl implements WorkLogInfoService {

    @Autowired
    private WorkLogInfoRepository workLogInfoRepository;

    @Autowired
    private WorkLogInfoMapper workLogInfoMapper;

    @Override
    public String insert(WorkLogInfo workLogInfo) {
        workLogInfo.setId(UUID.randomUUID().toString());
        if(workLogInfo.getCreatetime()==null){
            workLogInfo.setCreatetime(LocalDateTime.now());
        }
        workLogInfoRepository.save(workLogInfo);
        return "新增成功";
    }

    @Override
    public String update(WorkLogInfo workLogInfo) {
        workLogInfo.setUpdatetime(LocalDateTime.now());
        workLogInfoRepository.save(workLogInfo);
        return "更新成功";
    }

    @Override
    public String delete(WorkLogInfo workLogInfo) {
        workLogInfoRepository.delete(workLogInfo);
        return "删除成功";
    }

    @Override
    public PageInfo<WorkLogInfo> select(WorkLogInfo workLogInfo, MetaInfo metaInfo) {
        //PageHelper.startPage(metaInfo.getPage(),metaInfo.getSize());
        PageHelper.startPage(metaInfo.getPage(),metaInfo.getSize(),"createtime");
        List<WorkLogInfo> list=workLogInfoMapper.select(workLogInfo);
        PageInfo<WorkLogInfo> pageInfo=new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public WorkLogInfo get(String id) {
        Optional<WorkLogInfo> optional=workLogInfoRepository.findById(id);
        if(optional.isPresent()){
            return optional.get();
        }
        return null;
    }
}
